package com.zsc.externalservice.services.impl;

import com.zsc.externalservice.entity.AdsSjzlCcTbcProduct;
import com.zsc.externalservice.entity.Product;
import com.zsc.externalservice.entity.vo.CigaretteVO;
import com.zsc.externalservice.mapper.CigaretteMapper;
import com.zsc.externalservice.services.CigaretteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;

@Service
@Slf4j
public class CigaretteServiceImpl implements CigaretteService {

    @Resource
    private CigaretteMapper cigaretteMapper;

    /**
     * 计算毛利率
     *
     * @param retailPrice 市场零售价
     * @param wholesalePrice 批发价
     * @return String
     */
    public static String calculateGrossMargin(BigDecimal retailPrice, BigDecimal wholesalePrice) {
        if (wholesalePrice == null || retailPrice == null) {
            return "0%";
        }
        if (retailPrice.compareTo(BigDecimal.ZERO) == 0) {
            // 可以选择抛出自定义异常或返回默认值
            return "0%";
        }
        // 计算毛利
        BigDecimal grossProfit = retailPrice.subtract(wholesalePrice);
        // 计算毛利率
        BigDecimal grossMargin = grossProfit.divide(retailPrice, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100"));
        return grossMargin + "%";
    }

    /**
     * 根据名称查询卷烟数据
     * @param name 卷烟名称
     * @return Product
     */
    @Override
    public Product getCigaretteByName(String name) {
        Product product = cigaretteMapper.getCigaretteByName(name);

        if (product == null) {
            //数据库没查到数据，直接返回
            return new Product(name, null, null, null, null, null, null);
        }

        if (product.getPackageQty() == null || product.getPackageQty().compareTo(BigDecimal.ZERO) == 0) {
            //默认包包装支数为1
            product.setPackageQty(BigDecimal.ONE);
        }

        //计算每包的指导零售价、市场零售价和批发价
        BigDecimal directRetailPrice = product.getDirectRetailPrice().divide((product.getPackageQty2().divide(product.getPackageQty(), 2, RoundingMode.HALF_UP)), 2, RoundingMode.HALF_UP);
        BigDecimal packagePrice = product.getRetailPrice().divide((product.getPackageQty2().divide(product.getPackageQty(), 2, RoundingMode.HALF_UP)), 2, RoundingMode.HALF_UP);
        BigDecimal wholeSalePrice = product.getWholeSalePrice().divide((product.getPackageQty2().divide(product.getPackageQty(), 2, RoundingMode.HALF_UP)), 2, RoundingMode.HALF_UP);

        return Product.builder()
                .productName(product.getProductName())
                .productCode(product.getProductCode())
                .directRetailPrice(directRetailPrice)
                .retailPrice(packagePrice)
                .wholeSalePrice(wholeSalePrice)
                .packageQty(product.getPackageQty())
                .build();
    }

    /**
     * 根据卷烟代码code查询信息
     *
     * @param productCode 卷烟代码
     * @return CigaretteVO
     */
    @Override
    public CigaretteVO getByCode(String productCode) {
        AdsSjzlCcTbcProduct a = cigaretteMapper.getByCode(productCode);
        if (a == null) {
            return new CigaretteVO();
        }

        return CigaretteVO.builder()
                .productName(a.getProductName())
                .directRetailPrice(a.getDirectRetailPrice())
                .retailPrice(a.getRetailPrice())
                .wholeSalePrice(a.getWholeSalePrice())
                .grossProfitMargin(calculateGrossMargin(a.getRetailPrice(), a.getWholeSalePrice()))
                .entryTime(a.getSaleBeginDate())
                .priceType(a.getPriceTypeCodeText())
                .isOnSale(a.getIsActive())
                .factoryName(a.getFactorySimpleName())
                .tarQty(a.getTarQty())
                .nicotineQty(a.getNicotineQty())
                .productStyle(a.getProductStyleCodeText())
                .packageQty(String.valueOf(a.getPackageQty()))
                .packageQty2(String.valueOf(a.getPackageQty2()))
                .productCode(a.getProductCode())
                .build();
    }
}
